libgaim.framework [386] which has extensive debugging in the upnp module to try to...
[adiumx.git] / Plugins / Invite to Chat Plugin / DCInviteToChatPlugin.m
blob0f621273b1edbedcffb4510daeb131ffd0a27d05
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6  * General Public License as published by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11  * Public License for more details.
12  * 
13  * You should have received a copy of the GNU General Public License along with this program; if not,
14  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15  */
17 #import <Adium/AIContactControllerProtocol.h>
18 #import <Adium/AIInterfaceControllerProtocol.h>
19 #import <Adium/AIMenuControllerProtocol.h>
20 #import "DCInviteToChatPlugin.h"
21 #import "DCInviteToChatWindowController.h"
22 #import <AIUtilities/AIMenuAdditions.h>
23 #import <Adium/AIAccount.h>
24 #import <Adium/AIChat.h>
25 #import <Adium/AIListContact.h>
26 #import <Adium/AIListGroup.h>
27 #import <Adium/AIListObject.h>
28 #import <Adium/AIMetaContact.h>
29 #import <Adium/AIService.h>
31 #define INVITE_CONTACT                  AILocalizedString(@"Invite to Chat",nil)
33 @interface DCInviteToChatPlugin (PRIVATE)
34 - (NSMenu *)groupChatMenuForContact:(AIListContact *)contact;
35 @end
37 @implementation DCInviteToChatPlugin
39 - (void)installPlugin
41         
42         shouldRebuildChatList = YES;
43         
44         //Invite to Chat menu item
45         menuItem_inviteToChat = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:INVITE_CONTACT
46                                                                                                                                                                   target:self
47                                                                                                                                                                   action:@selector(dummyTarget:)
48                                                                                                                                                    keyEquivalent:@""] autorelease];
49         [[adium menuController] addMenuItem:menuItem_inviteToChat toLocation:LOC_Contact_Action];
50         
51         //Invite to Chat context menu item
52         menuItem_inviteToChatContext = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:INVITE_CONTACT
53                                                                                                                                                                                  target:self
54                                                                                                                                                                                  action:@selector(dummyTarget:)
55                                                                                                                                                                   keyEquivalent:@""] autorelease];
56         [[adium menuController] addContextualMenuItem:menuItem_inviteToChatContext toLocation:Context_Contact_Action];  
57         
60 //Validate our menu items
61 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
62 {                
63         if (menuItem == menuItem_inviteToChat) {                
65                 AIListObject *object = [[adium interfaceController] selectedListObjectInContactList];
67                 if ([object isKindOfClass:[AIListContact class]]) {
68                         if ( shouldRebuildChatList ) {
69                                 [menuItem_inviteToChat setSubmenu:[self groupChatMenuForContact:(AIListContact *)object]];
70                         }
71                      
72                         return ([[menuItem_inviteToChat submenu] numberOfItems] > 0);
73                 } else {
74                         [menuItem_inviteToChat setTitle:INVITE_CONTACT]; // hit the reset button
75                         return NO;
76                 }
77                 
78         } else if ( menuItem == menuItem_inviteToChatContext ) {
79                 
80                 if ( shouldRebuildChatList ) {
81                         AIListObject *object = [[adium menuController] currentContextMenuObject];
82                         if ([object isKindOfClass:[AIListContact class]]) {
83                                 [menuItem_inviteToChatContext setSubmenu:[self groupChatMenuForContact:(AIListContact *)object]];
84                         }
85                 }
86                 return ([[menuItem_inviteToChatContext submenu] numberOfItems] > 0);
88         }
89         
90         return YES;
93 - (IBAction)inviteToChat:(id)sender
95         NSArray                 *repArray = [sender representedObject];
96         AIListContact   *listContact = [repArray objectAtIndex:1];
97         AIChat                  *chat = [repArray objectAtIndex:0];
98         
99         [DCInviteToChatWindowController inviteToChatWindowForChat:chat contact:listContact];
102 #pragma mark Private
104 - (NSMenu *)groupChatMenuForContact:(AIListContact *)contact
106         NSArray                 *openChats = [[adium interfaceController] openChats];
107         AIChat                  *chat;
108         NSMenu                  *menu_chatMenu = nil;
109         NSDictionary    *serviceDict;
110         NSString                *serviceClass;
111         
112         if (contact && ![contact isKindOfClass:[AIListGroup class]]) {
113                 NSEnumerator *enumerator;
114                 unsigned        currentNumberOfItems, numberOfMenuItems = 0;
115                 
116                 // Get a dictionary of (service class, contacts in that service)
117                 serviceDict = ([contact isKindOfClass:[AIMetaContact class]] ?
118                                            [(AIMetaContact *)contact dictionaryOfServiceClassesAndListContacts] :
119                                            [NSDictionary dictionaryWithObject:contact forKey:[[contact service] serviceClass]]);
121                 [menu_chatMenu setMenuChangedMessagesEnabled:NO];
123                 enumerator = [serviceDict keyEnumerator];
124                 while ((serviceClass = [enumerator nextObject])) {
125                         
126                         //Each iteration, if we have more menu items now than before, add a separator item
127                         currentNumberOfItems = [menu_chatMenu numberOfItems];
128                         if (currentNumberOfItems > numberOfMenuItems) {
129                                 [menu_chatMenu addItem:[NSMenuItem separatorItem]];
130                                 numberOfMenuItems = currentNumberOfItems + 1;
131                         }
132                         
133                         // Loop through all chats
134                         for (int i = 0; i < [openChats count]; i++) {
135                                 chat = [openChats objectAtIndex:i];
136                                 
137                                 // Is this the same serviceClass as this contact?                               
138                                 if ( [[[[chat account] service] serviceClass] isEqualToString:serviceClass] ) {
139                                         
140                                         // Is this a group chat?
141                                         if ([chat isGroupChat]) {
142                                                 if (!menu_chatMenu) {
143                                                         menu_chatMenu = [[[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:@""] autorelease];
144                                                 }
145                                                 
146                                                 if ( [menu_chatMenu indexOfItemWithTitle:[chat name]] == -1 ) {
147                                                         NSMenuItem *menuItem;
148                                                         menuItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:[chat name]
149                                                                                                                                                                                         target:self
150                                                                                                                                                                                         action:@selector(inviteToChat:)
151                                                                                                                                                                          keyEquivalent:@""];
152                                                         [menuItem setRepresentedObject:[NSArray arrayWithObjects:chat,contact,nil]];
153                                                         [menu_chatMenu addItem:menuItem];
154                                                         [menuItem release];
155                                                 }
156                                         }
157                                 }
158                         }
159                 }
160                 
161                 //Remove the last separator if our new number of items isn't bigger than the previous one (that is, we haven't added any items since the last separator)
162                 currentNumberOfItems = [menu_chatMenu numberOfItems];
163                 if ((currentNumberOfItems <= numberOfMenuItems) &&
164                         (currentNumberOfItems > 0)) {
165                         
166                         [menu_chatMenu removeItemAtIndex:(currentNumberOfItems-1)];
167                 }
168                 
169                 [menu_chatMenu setMenuChangedMessagesEnabled:YES];
170         }
171         
172         return menu_chatMenu;
175 // Dummy target so that we get validateMenuItem calls
176 - (IBAction)dummyTarget:(id)sender { }
177         
178 @end